﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Data;
using UtilReport;
using UtilReportOMS.Class;
namespace UtilReportOMS
{
    public class clsThongTinTrinhKy
    {
        public string mv_TEN_BIEUBC = "";
        public string mv_TEN_DOITUONG = "";
        public Int32 mv_CHIEU_RONG = 0;
        public Int32 mv_CHIEU_DAI = 0;
        public Int32 mv_TOADO_DOC = 0;
        public Int32 mv_TOADO_NGANG = 0;
        public string mv_FONT_CHU = "Arial";
        public Int32 mv_CO_CHU = 12;
        public string mv_KIEU_CHU = "Regular";
        public string mv_NOI_DUNG = "";
        public string mv_MA_DVIQLY = "";
        public string mv_FIELD_NAME = "";
        public string mv_FORMULA_NAME = "";
        public DateTime? mv_NGAY_CAPNHAT = null;
        private bool exists = false;

        //public static string FILE_NAME = "TRINH_KY";
        public static string TEN_BIEUBC= "TEN_BIEUBC"   ;
        public static string TEN_DOITUONG= "TEN_DOITUONG"   ;
        public static string CHIEU_DAI= "CHIEU_DAI"   ;
        public static string CHIEU_RONG= "CHIEU_RONG"   ;
        public static string TOADO_DOC= "TOADO_DOC"   ;
        public static string TOADO_NGANG= "TOADO_NGANG"   ;
        public static string FONT_CHU= "FONT_CHU"   ;
        public static string KIEU_CHU= "KIEU_CHU"   ;
        public static string NOI_DUNG= "NOI_DUNG"   ;
        public static string MA_DVIQLY= "MA_DVIQLY"   ;
        public static string NGAY_CAPNHAT= "NGAY_CAPNHAT"   ;
        public static string CO_CHU = "CO_CHU";
        public static string FORMULA_NAME = "FORMULA_NAME";
        public static string FIELD_NAME = "FIELD_NAME";
        public static DataSet dsTrinhKy; 
        public bool Exists
        {
            get { return exists; }
            set { exists = value; }
        }

        public clsThongTinTrinhKy()
        {
        }
        // tao cac phuong thuc
        /// <summary>
        /// Lấy trình ký trên báo cáo
        /// </summary>
        /// <param name="TenBieuBaoCao">Tên biểu báo cáo</param>
        /// <param name="rpt">Đối tượng trình ký</param>
        /// <param name="pv_sContent">Nội dung trình ký</param>
        /// <returns></returns>
        public List<clsThongTinTrinhKy> getTrinhKyOnRpt(string TenBieuBaoCao, string sv_sDonVi, CrystalDecisions.CrystalReports.Engine.ReportDocument rptdoc, string[] field_name, string[] formula_name)
        {            
            List<clsThongTinTrinhKy> l_tt_tk = new List<clsThongTinTrinhKy>();
            for (int i = 0; i < field_name.Length;++i )
            {
                CrystalDecisions.CrystalReports.Engine.FieldObject mv_oRptFieldObj = (CrystalDecisions.CrystalReports.Engine.FieldObject)rptdoc.ReportDefinition.ReportObjects[field_name[i]];
                if (mv_oRptFieldObj == null)
                {
                    continue;
                }
                if (rptdoc.DataDefinition.FormulaFields[formula_name[i]].Text == null)
                {
                    rptdoc.DataDefinition.FormulaFields[formula_name[i]].Text = " ";
                }
                string noidung = rptdoc.DataDefinition.FormulaFields[formula_name[i]].Text.Replace("'", "").Replace("&Chr(13)&","\r\n");
                try
                {
                    clsThongTinTrinhKy c = new clsThongTinTrinhKy();
                    //gan cac thong tin co duoc vao bien
                    c.mv_TEN_BIEUBC = TenBieuBaoCao;// 'rpt.ToString
                    c.mv_TEN_DOITUONG = mv_oRptFieldObj.Name;
                    c.mv_CHIEU_RONG = mv_oRptFieldObj.Height;
                    c.mv_CHIEU_DAI = mv_oRptFieldObj.Width;
                    c.mv_TOADO_DOC = mv_oRptFieldObj.Top;
                    c.mv_TOADO_NGANG = mv_oRptFieldObj.Left;
                    c.mv_FONT_CHU = mv_oRptFieldObj.Font.Name;
                    c.mv_CO_CHU = Convert.ToInt32(mv_oRptFieldObj.Font.Size);
                    switch (mv_oRptFieldObj.Font.Style)
                    {
                        case FontStyle.Bold:
                            c.mv_KIEU_CHU = "Chữ đậm";
                            break;
                        case FontStyle.Italic:
                            c.mv_KIEU_CHU = "Chữ nghiêng";
                            break;
                        case FontStyle.Regular:
                            c.mv_KIEU_CHU = "Bình thường";
                            break;
                        case FontStyle.Strikeout:
                            c.mv_KIEU_CHU = "Gạch giữa";
                            break;
                        case FontStyle.Underline:
                            c.mv_KIEU_CHU = "Gạch chân";
                            break;
                        default:
                            c.mv_KIEU_CHU = "Bình thường";
                            break;
                    }
                    c.mv_NOI_DUNG = noidung.Replace(@"""", "");
                    c.mv_MA_DVIQLY = sv_sDonVi;
                    c.mv_NGAY_CAPNHAT = DateTime.Now;
                    c.mv_FIELD_NAME = field_name[i];
                    c.mv_FORMULA_NAME = formula_name[i];
                    l_tt_tk.Add(c);                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return l_tt_tk;
        }

        public void setForntToRPT(ref CrystalDecisions.CrystalReports.Engine.FieldObject rpt)
        {
            Font ft;
            try
            {
                switch (mv_KIEU_CHU.Trim().ToUpper())
                {
                    case "CHỮ ĐẬM":
                        ft = new Font(mv_FONT_CHU, mv_CO_CHU, FontStyle.Bold);
                        break;
                    case "CHỮ NGHIÊNG":
                        ft = new Font(mv_FONT_CHU, mv_CO_CHU, FontStyle.Italic);
                        break;
                    case "BÌNH THƯỜNG":
                        ft = new Font(mv_FONT_CHU, mv_CO_CHU, FontStyle.Regular);
                        break;
                    case "Gạch giữa":
                        ft = new Font(mv_FONT_CHU, mv_CO_CHU, FontStyle.Strikeout);
                        break;
                    case "Gạch chân":
                        ft = new Font(mv_FONT_CHU, mv_CO_CHU, FontStyle.Underline);
                        break;
                    default:
                        ft = new Font(mv_FONT_CHU, mv_CO_CHU, FontStyle.Regular);
                        break;
                }
                rpt.ApplyFont(ft);
            }
            catch (Exception ex)
            {
                ft = new Font("Arial", 13, FontStyle.Regular);
                throw ex;
            }
            
        }

        //ham nay se thuc hien lay thong tin tren server ve
        public List<clsThongTinTrinhKy> getTrinhKyOnDb(string sv_sBieuMau, string sv_sDonVi)
        {
            try
            {
                List<clsThongTinTrinhKy> LTT_TK = new List<clsThongTinTrinhKy>();
                DataSet dsTrinhKy =  GlobalServices.Service_NPCUS.SelectTrinhKy(sv_sDonVi, sv_sBieuMau);
                if (dsTrinhKy.Tables[0] != null)
                {
                    if (dsTrinhKy.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in dsTrinhKy.Tables[0].Rows)
                        {
                            clsThongTinTrinhKy t = new clsThongTinTrinhKy();
                            t.mv_TEN_BIEUBC = dr[TEN_BIEUBC].ToString();// 'rpt.ToString
                            t.mv_TEN_DOITUONG = dr[TEN_DOITUONG].ToString();
                            t.mv_CHIEU_RONG = Convert.ToInt32(dr[CHIEU_RONG]);
                            t.mv_CHIEU_DAI = Convert.ToInt32(dr[CHIEU_DAI]);
                            t.mv_TOADO_DOC = Convert.ToInt32(dr[TOADO_DOC]);
                            t.mv_TOADO_NGANG = Convert.ToInt32(dr[TOADO_NGANG]);
                            t.mv_FONT_CHU = dr[FONT_CHU].ToString();
                            t.mv_CO_CHU = Convert.ToInt32(dr[CO_CHU].ToString());
                            t.mv_KIEU_CHU = dr[KIEU_CHU].ToString();
                            t.mv_NOI_DUNG = dr[NOI_DUNG].ToString().Replace("'", "");
                            t.mv_MA_DVIQLY = dr[MA_DVIQLY].ToString();
                            t.mv_FORMULA_NAME = dr[FORMULA_NAME].ToString();
                            t.mv_FIELD_NAME = dr[FIELD_NAME].ToString();
                            t.Exists = true;
                            LTT_TK.Add(t);
                        }
                    }
                }
                return LTT_TK;
            }                
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // ham nay se thuc hien update thong tin tren server ve
        public bool updateTrinhKyOnDb(List<clsThongTinTrinhKy> c)
        {
            try
            {
                //TKEntities.STTIN_TRINHKY_Entity entity = new TKEntities.STTIN_TRINHKY_Entity();              
                dsTrinhKy = new DataSet();
                dsTrinhKy.Tables.Add(new DataTable("TRINH KY"));
                DataTable dt = dsTrinhKy.Tables[0];
                dt.Columns.Add(new DataColumn(TEN_BIEUBC));
                dt.Columns.Add(new DataColumn(TEN_DOITUONG));
                dt.Columns.Add(new DataColumn(CHIEU_RONG));
                dt.Columns.Add(new DataColumn(CHIEU_DAI));
                dt.Columns.Add(new DataColumn(TOADO_DOC));
                dt.Columns.Add(new DataColumn(TOADO_NGANG));
                dt.Columns.Add(new DataColumn(FONT_CHU));
                dt.Columns.Add(new DataColumn(CO_CHU));
                dt.Columns.Add(new DataColumn(KIEU_CHU));
                dt.Columns.Add(new DataColumn(NOI_DUNG));
                dt.Columns.Add(new DataColumn(MA_DVIQLY));
                dt.Columns.Add(new DataColumn(NGAY_CAPNHAT));
                dt.Columns.Add(new DataColumn(FORMULA_NAME));
                dt.Columns.Add(new DataColumn(FIELD_NAME));

                foreach (clsThongTinTrinhKy ctt_tk in c)
                {
                    DataRow dr = dt.NewRow();
                    dr[TEN_BIEUBC] = ctt_tk.mv_TEN_BIEUBC;
                    dr[TEN_DOITUONG] = ctt_tk.mv_TEN_DOITUONG;
                    dr[CHIEU_RONG] = ctt_tk.mv_CHIEU_RONG;
                    dr[CHIEU_DAI] = ctt_tk.mv_CHIEU_DAI;
                    dr[TOADO_DOC] = ctt_tk.mv_TOADO_DOC;
                    dr[TOADO_NGANG] = ctt_tk.mv_TOADO_NGANG;
                    dr[FONT_CHU] = ctt_tk.mv_FONT_CHU;
                    dr[CO_CHU] = ctt_tk.mv_CO_CHU;
                    dr[KIEU_CHU] = ctt_tk.mv_KIEU_CHU;
                    dr[NOI_DUNG] = ctt_tk.mv_NOI_DUNG;
                    dr[MA_DVIQLY] = ctt_tk.mv_MA_DVIQLY;
                    dr[NGAY_CAPNHAT] = DateTime.Now;
                    dr[FORMULA_NAME] = ctt_tk.mv_FORMULA_NAME;
                    dr[FIELD_NAME] = ctt_tk.mv_FIELD_NAME;
                    dt.Rows.Add(dr);                                      
                }

                GlobalServices.Service_NPCUS.SaveTrinhKy(dsTrinhKy);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;                
            }
        }

        public bool insertTrinhKyOnDb(List<clsThongTinTrinhKy> c)
        {
            try
            {
                //TKEntities.STTIN_TRINHKY_Entity entity = new TKEntities.STTIN_TRINHKY_Entity();             
                dsTrinhKy = new DataSet();
                dsTrinhKy.Tables.Add(new DataTable("TRINH KY"));
                DataTable dt = dsTrinhKy.Tables[0];
                dt.Columns.Add(new DataColumn(TEN_BIEUBC));
                dt.Columns.Add(new DataColumn(TEN_DOITUONG));
                dt.Columns.Add(new DataColumn(CHIEU_RONG));
                dt.Columns.Add(new DataColumn(CHIEU_DAI));
                dt.Columns.Add(new DataColumn(TOADO_DOC));
                dt.Columns.Add(new DataColumn(TOADO_NGANG));
                dt.Columns.Add(new DataColumn(FONT_CHU));
                dt.Columns.Add(new DataColumn(CO_CHU));
                dt.Columns.Add(new DataColumn(KIEU_CHU));
                dt.Columns.Add(new DataColumn(NOI_DUNG));
                dt.Columns.Add(new DataColumn(MA_DVIQLY));
                dt.Columns.Add(new DataColumn(NGAY_CAPNHAT));
                dt.Columns.Add(new DataColumn(FORMULA_NAME));
                dt.Columns.Add(new DataColumn(FIELD_NAME));
                foreach (clsThongTinTrinhKy ctt_tk in c)
                {                    
                        DataRow dr = dt.NewRow();
                        dr[TEN_BIEUBC] = ctt_tk.mv_TEN_BIEUBC;
                        dr[TEN_DOITUONG] = ctt_tk.mv_TEN_DOITUONG;
                        dr[CHIEU_RONG] = ctt_tk.mv_CHIEU_RONG;
                        dr[CHIEU_DAI] = ctt_tk.mv_CHIEU_DAI;
                        dr[TOADO_DOC] = ctt_tk.mv_TOADO_DOC;
                        dr[TOADO_NGANG] = ctt_tk.mv_TOADO_NGANG;
                        dr[FONT_CHU] = ctt_tk.mv_FONT_CHU;
                        dr[CO_CHU] = ctt_tk.mv_CO_CHU;
                        dr[KIEU_CHU] = ctt_tk.mv_KIEU_CHU;
                        dr[NOI_DUNG] = ctt_tk.mv_NOI_DUNG;
                        dr[MA_DVIQLY] = ctt_tk.mv_MA_DVIQLY;
                        dr[NGAY_CAPNHAT] = ctt_tk.mv_NGAY_CAPNHAT;
                        dr[FORMULA_NAME] = ctt_tk.mv_FORMULA_NAME;
                        dr[FIELD_NAME] = ctt_tk.mv_FIELD_NAME;
                        dr[NGAY_CAPNHAT] = DateTime.Now;
                        dt.Rows.Add(dr);
                }
                //dsTrinhKy.WriteXml(@"C:\CMIS2Config\TRINH_KY\" + c[0].mv_TEN_BIEUBC + ".xml");
                //bus_TienichNPC.clsTrinhKy_bus bus = new bus_TienichNPC.clsTrinhKy_bus();
                //bus.InsertTrinhKy(dsTrinhKy);
                GlobalServices.Service_NPCUS.InsertTrinhKy(dsTrinhKy);
                return true;
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool setChangeInRPT(List<clsThongTinTrinhKy> L_tt_tk, CrystalDecisions.CrystalReports.Engine.ReportDocument rptdoc)
        {
            try
            {
                foreach (clsThongTinTrinhKy c in L_tt_tk)
                {
                    CrystalDecisions.CrystalReports.Engine.FieldObject mv_oRptFieldObj = (CrystalDecisions.CrystalReports.Engine.FieldObject)rptdoc.ReportDefinition.ReportObjects[c.mv_FIELD_NAME];
                    //System.Threading.Thread.Sleep(1000);                    
                    rptdoc.DataDefinition.FormulaFields[c.mv_FORMULA_NAME].Text = Convert.ToChar(34) +
                        c.mv_NOI_DUNG.Replace("\r\n", Convert.ToChar(34) + "&Chr(13)&" + Convert.ToChar(34)) + Convert.ToChar(34);
                    //System.Threading.Thread.Sleep(1000);
                    c.setForntToRPT(ref mv_oRptFieldObj);
                    //mv_oRptFieldObj.Dispose();
                    //mv_oRptFieldObj.Height = c.mv_CHIEU_DAI;
                    //mv_oRptFieldObj.Width=c.mv_CHIEU_RONG;
                    //mv_oRptFieldObj.Top = c.mv_TOADO_DOC;
                    //mv_oRptFieldObj.Left = c.mv_TOADO_NGANG;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #region Process with XML
        public static DataSet ReadXMLTrinhKy(string sv_sDonVi,string sv_sBieuMau)
        {           
            try
            {
                 DataSet dsXML = new DataSet();
                 dsXML.ReadXml(@"C:\CMIS2Config\TRINH_KY\" + sv_sBieuMau+".xml");
                 return dsXML;
            }
            catch
            {
                return null;
            }

        }
        #endregion
    }
       
}
